package com.gexiaobao.pigeon.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.BobInfoResponse
import com.gexiaobao.pigeon.ui.adapter.ShedListAdapter
import com.gexiaobao.pigeon.ui.view.RecycleViewDivider

/**
 * @Author      : hxw
 * @Date        : 2024/4/29 11:25
 * @Describe    :
 */
class RxShedListDialog(context: Context?) : RxDialog(context!!) {

    private lateinit var cancelView: AppCompatImageView
    private lateinit var rvShedList: RecyclerView
    private val mAdapter: ShedListAdapter by lazy { ShedListAdapter(arrayListOf()) }

    private var onShedNameClickListener: OnShedNameClickListener? = null

    init {
        initView()
    }

    @SuppressLint("MissingInflatedId")
    private fun initView() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_shed_list, null)
        cancelView = dialogView.findViewById(R.id.tvCancel)
        rvShedList = dialogView.findViewById(R.id.rvShedList)
        cancelView.setOnClickListener {
            dismiss()
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            if (onShedNameClickListener != null) {
                onShedNameClickListener!!.onItemClick(
                    position,
                    mAdapter.data[position].name,
                    mAdapter.data[position].userId,
                    mAdapter.data[position].id
                )
            }
            dismiss()
        }
        setContentView(dialogView)
    }

    fun setData(dataResult: ArrayList<BobInfoResponse.ListData>) {
        rvShedList.layoutManager = LinearLayoutManager(context)
        rvShedList.adapter = mAdapter
        rvShedList.addItemDecoration(RecycleViewDivider(context, LinearLayoutManager.VERTICAL, 1, ContextCompat.getColor(context, R.color.comm_bg)))
        mAdapter.setList(dataResult)
    }

    fun setOnShedNameClickListener(listener: OnShedNameClickListener?) {
        onShedNameClickListener = listener
    }

    interface OnShedNameClickListener {
        fun onItemClick(position: Int, name: String, userId: Int, id: Int)
    }
}